using System;
using EPiServer.Common;
using EPiServer.Common.Rating;
using EPiServer.Common.Security;
using EPiServer.Community.Contact;
using EPiServer.Community.Contact.Queries;

namespace EPiAbstractions.Community.Contact
{
    public class ContactHandlerFacade : IContactHandlerFacade
    {
        private static ContactHandlerFacade _instance;

        public ContactHandlerFacade()
        {
            ContactHandler.ContactRelationAdded += OnContactRelationAdded;
            ContactHandler.ContactRelationUpdated += OnContactRelationUpdated;
            ContactHandler.ContactRelationRemoved += OnContactRelationRemoved;
        }

        public static ContactHandlerFacade Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new ContactHandlerFacade();

                return _instance;
            }

            set { _instance = value; }
        }

        #region IContactHandlerFacade Members

        public event EPiServerCommonEventHandler ContactRelationAdded;
        public event EPiServerCommonEventHandler ContactRelationUpdated;
        public event EPiServerCommonEventHandler ContactRelationRemoved;

        public virtual ContactContainer AddContactContainer(ContactContainer contactContainer)
        {
            return ContactHandler.AddContactContainer(contactContainer);
        }

        public virtual ContactContainer GetContactContainer(IUser owner)
        {
            return ContactHandler.GetContactContainer(owner);
        }

        public virtual ContactContainer GetContactContainer(IUser owner, ISite site)
        {
            return ContactHandler.GetContactContainer(owner, site);
        }

        public virtual ContactContainer GetContactContainer(IUser owner, ISite site, Boolean createContainer)
        {
            return ContactHandler.GetContactContainer(owner, site, createContainer);
        }

        public virtual ContactContainer GetContactContainer(Int32 id)
        {
            return ContactHandler.GetContactContainer(id);
        }

        public virtual ContactContainerCollection GetContactContainers(IUser owner)
        {
            return ContactHandler.GetContactContainers(owner);
        }

        public virtual ContactRelation AddContactRelation(ContactRelation contactRelation)
        {
            return ContactHandler.AddContactRelation(contactRelation);
        }

        public virtual void UpdateContactRelation(ContactRelation contactRelation)
        {
            ContactHandler.UpdateContactRelation(contactRelation);
        }

        public virtual void RemoveContactRelation(ContactRelation contactRelation)
        {
            ContactHandler.RemoveContactRelation(contactRelation);
        }

        public virtual ContactRelation GetContactRelation(Int32 id)
        {
            return ContactHandler.GetContactRelation(id);
        }

        public virtual ContactRelation GetContactRelation(ContactContainer contactContainer, IUser userContact)
        {
            return ContactHandler.GetContactRelation(contactContainer, userContact);
        }

        public virtual ContactRelationCollection GetContacts(ContactContainer contactContainer, ContactType type, Perspective perspective,
                                                             Int32 page, Int32 pageSize, ContactRelationSortOrder[] orderBy)
        {
            return ContactHandler.GetContacts(contactContainer, type, perspective, page, pageSize, orderBy);
        }

        public virtual ContactRelationCollection GetContacts(ContactContainer contactContainer, ContactType type, Perspective perspective,
                                                             Int32 page, Int32 pageSize, out Int32 totalItems,
                                                             ContactRelationSortOrder[] orderBy)
        {
            return ContactHandler.GetContacts(contactContainer, type, perspective, page, pageSize, out totalItems, orderBy);
        }

        public virtual ContactRelationCollection GetContacts(IUser owner, ContactType type, Perspective perspective, Int32 page,
                                                             Int32 pageSize, ContactRelationSortOrder[] orderBy)
        {
            return ContactHandler.GetContacts(owner, type, perspective, page, pageSize, orderBy);
        }

        public virtual ContactRelationCollection GetContacts(IUser owner, ContactType type, Perspective perspective, Int32 page,
                                                             Int32 pageSize, out Int32 totalItems, ContactRelationSortOrder[] orderBy)
        {
            return ContactHandler.GetContacts(owner, type, perspective, page, pageSize, out totalItems, orderBy);
        }

        public virtual ContactRelationCollection GetContacts(IUser owner, ContactType type, Perspective perspective, Int32 page,
                                                             Int32 pageSize, SiteCollection sites, ContactRelationSortOrder[] orderBy)
        {
            return ContactHandler.GetContacts(owner, type, perspective, page, pageSize, sites, orderBy);
        }

        public virtual ContactRelationCollection GetContacts(IUser owner, ContactType type, Perspective perspective, Int32 page,
                                                             Int32 pageSize, SiteCollection sites, out Int32 totalItems,
                                                             ContactRelationSortOrder[] orderBy)
        {
            return ContactHandler.GetContacts(owner, type, perspective, page, pageSize, sites, out totalItems, orderBy);
        }

        public virtual RatingCollection GetContactsRatedItems(ContactContainer contactContainer, Int32 page, Int32 pageSize)
        {
            return ContactHandler.GetContactsRatedItems(contactContainer, page, pageSize);
        }

        public virtual RatingCollection GetContactsRatedItems(ContactContainer contactContainer, Int32 page, Int32 pageSize,
                                                              out Int32 totalItems)
        {
            return ContactHandler.GetContactsRatedItems(contactContainer, page, pageSize, out totalItems);
        }

        public virtual RatingCollection GetContactsRatedItems(ContactContainer contactContainer, Int32 page, Int32 pageSize,
                                                              out Int32 totalItems, Type objectType)
        {
            return ContactHandler.GetContactsRatedItems(contactContainer, page, pageSize, out totalItems, objectType);
        }

        public virtual ContactRelationCollection GetContactsOnline(ContactContainer contactContainer, ContactType contactType,
                                                                   Perspective perspective, Int32 page, Int32 pageSize, out Int32 totalItems,
                                                                   ContactRelationSortOrder[] orderBy)
        {
            return ContactHandler.GetContactsOnline(contactContainer, contactType, perspective, page, pageSize, out totalItems, orderBy);
        }

        public virtual ContactRelationCollection GetContactsOnline(IUser owner, ContactType type, Perspective perspective, Int32 page,
                                                                   Int32 pageSize, out Int32 totalItems, SiteCollection sites,
                                                                   ContactRelationSortOrder[] orderBy)
        {
            return ContactHandler.GetContactsOnline(owner, type, perspective, page, pageSize, out totalItems, sites, orderBy);
        }

        public virtual ContactRelationCollection GetContactsOnline(IUser owner, ContactType type, Perspective perspective, Int32 page,
                                                                   Int32 pageSize, SiteCollection sites, out Int32 totalItems,
                                                                   ContactRelationSortOrder[] orderBy)
        {
            return ContactHandler.GetContactsOnline(owner, type, perspective, page, pageSize, sites, out totalItems, orderBy);
        }

        public virtual ContactContainerCollection GetQueryResult(ContactContainerQuery query)
        {
            return ContactHandler.GetQueryResult(query);
        }

        public virtual ContactContainerCollection GetQueryResult(ContactContainerQuery query, Int32 page, Int32 pageSize)
        {
            return ContactHandler.GetQueryResult(query, page, pageSize);
        }

        public virtual ContactContainerCollection GetQueryResult(ContactContainerQuery query, Int32 page, Int32 pageSize,
                                                                 out Int32 totalItems)
        {
            return ContactHandler.GetQueryResult(query, page, pageSize, out totalItems);
        }

        public virtual ContactRelationCollection GetQueryResult(ContactRelationQuery query)
        {
            return ContactHandler.GetQueryResult(query);
        }

        public virtual ContactRelationCollection GetQueryResult(ContactRelationQuery query, Int32 page, Int32 pageSize)
        {
            return ContactHandler.GetQueryResult(query, page, pageSize);
        }

        public virtual ContactRelationCollection GetQueryResult(ContactRelationQuery query, Int32 page, Int32 pageSize, out Int32 totalItems)
        {
            return ContactHandler.GetQueryResult(query, page, pageSize, out totalItems);
        }

        #endregion

        public virtual void OnContactRelationAdded(String sender, EPiServerCommonEventArgs e)
        {
            if (ContactRelationAdded != null)
                ContactRelationAdded(sender, e);
        }

        public virtual void OnContactRelationUpdated(String sender, EPiServerCommonEventArgs e)
        {
            if (ContactRelationUpdated != null)
                ContactRelationUpdated(sender, e);
        }

        public virtual void OnContactRelationRemoved(String sender, EPiServerCommonEventArgs e)
        {
            if (ContactRelationRemoved != null)
                ContactRelationRemoved(sender, e);
        }
    }
}